package com.qqtech.qquan.user.service;

import java.sql.Timestamp;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.user.dao.UserAuthDao;
import com.qqtech.qquan.user.dao.UserBaseDao;
import com.qqtech.qquan.user.dao.UserLoginDao;
import com.qqtech.qquan.user.enums.UserAuthAuditStatusDictEnum;
import com.qqtech.qquan.user.model.UserAuth;
import com.qqtech.qquan.user.model.UserAuthQuery;
import com.qqtech.qquan.user.model.UserAuthVo;
import com.qqtech.qquan.user.model.UserBase;
import com.qqtech.qquan.user.model.UserLogin;

@Service
public class UserAuthServiceImpl extends BaseServiceImpl<UserAuth>implements UserAuthService {
	public static final Logger log = LoggerFactory.getLogger(UserAuthServiceImpl.class);
	@Resource
	private UserLoginService userLoginService;
	@Resource
	private UserAuthDao userAuthDao;

	@Resource
	private UserBaseDao userBaseDao;

	@Resource
	private UserLoginDao userLoginDao;

	@Override
	protected BaseDao<UserAuth> getBaseDao() {
		return userAuthDao;
	}

	@Override
	public UserAuthVo getByUserId(java.lang.Integer userId) {
		return userAuthDao.getByUserId(userId);
	}
	
	@Override
	public Page<UserAuthVo> queryPageListAdmin(UserAuthQuery query, Pageable pageable) {
		return userAuthDao.queryPageListAdmin(query, pageable);
	}

	@Override
	public Result handleThrough(UserAuth userAuth, Integer adminUserId) {
		Result result = new Result();
		Integer userId = userAuth.getUserId();
		String birthPlace = userAuth.getBirthPlace();
		Timestamp birthAt = userAuth.getBirthAt();
		if (userId == null || adminUserId == null) {
			result.addErr("参数不能为空");
		} else if (StringUtil.isBlank(birthPlace)) {
			result.addErr("籍贯不能为空");
		} else if (birthAt == null) {
			result.addErr("出生年日不能为空");
		} else {
			userAuth = userAuthDao.getByUserId(userId);
			if (userAuth == null) {
				result.addErr("未申请身份认证");
			} else if (userAuth.getAuditStatus().intValue() == UserAuthAuditStatusDictEnum.THROUGH.getCode()) {
				result.addErr("请不要重复认证");
			}
		}
		if (result.checkForm()) {
			UserAuth update = new UserAuth();
			update.setId(userAuth.getId());
			update.setAdminUserId(adminUserId);
			update.setAuditStatus(UserAuthAuditStatusDictEnum.THROUGH.getCode());
			update.setAuditStatusAt(TimeUtil.now());
			update.setUpdAt(update.getAuditStatusAt());
			update.setBirthAt(birthAt);
			update.setBirthPlace(birthPlace);
			userAuthDao.updateById(update);
			UserBase userBase = new UserBase();
			userBase.setId(userId);
			userBase.setHasIdentityAuth(YesNoEnum.YES.getCode());
			userBaseDao.updateById(userBase);
			UserLogin userLogin = userLoginService.getById(userId);
			UserLogin updateUserLogin = new UserLogin();
			updateUserLogin.setId(userId);
			updateUserLogin.setHasIdentityAuth(YesNoEnum.YES.getCode());
			userLoginDao.updateById(updateUserLogin);
			if (userLogin != null && StringUtil.isNotBlank(userLogin.getToken())) {
				userLoginService.clear(userLogin.getToken());
				userLoginService.getByToken(userLogin.getToken());
			}
		}
		return result;
	}
}